<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String deptId = request.getParameter("deptId");
	String did    = (deptId==null || deptId.equals("e847c9e8-10d2-33c5-cdba-81b243ef7916"))?"":deptId;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<jsp:include page="../meta.jsp"></jsp:include>  
	<jsp:include page="../easyUI/easyUI.jsp"></jsp:include>
	
	<style type="text/css">
	html body {font-size:12px;}
	</style>
    
     <script type="text/javascript">
     
     var editRow;
	 var datagrid;
	 var userDialog;
	 var userRoleDialog;
	 var url;
	 var userForm;
	 var userRoleForm;
	 var passwordInput;
	 var proPath;
	
$(function()
{
    
    var proPath = "<%=request.getContextPath()%>";
	userForm = $('#userForm').form();

	passwordInput = userForm.find('[name=loginPass]').validatebox({
		required : true
	});
	
	
	$('[name=roleId]').combotree({
			url : proPath+'/roleController.do?tree',
			animate : false,
			lines : !sy.isLessThanIe8(),
			checkbox : true,
			multiple : true,
			onLoadSuccess : function(node, data) {
				var t = $(this);
				if (data) {
					$(data).each(function(index, d) {
						if (this.state == 'closed') {
							t.tree('expandAll');
						}
					});
				}
			}
		});
	
	userRoleDialog = $('#userRoleDialog').show().dialog({
			modal : true,
			title : '批量编辑用户角色',
			buttons : [ {
				text : '编辑',
				handler : function() {
					userRoleForm.submit();
				}
			} ]
		}).dialog('close');

	userRoleForm = $('#userRoleForm').form({
		url : proPath+'/userController.do?editUsersRole',
		success : function(data) {
			var d = $.parseJSON(data);
			if (d) {
				userRoleDialog.dialog('close');
				$.messager.show({
					msg : '批量修改用户角色成功！',
					title : '提示'
				});
				datagrid.datagrid('reload');
			}
		}
	});
	
		
	userDialog = $('#userDialog').show().dialog({
			modal : true,
			collapsible:true,
			maximizable:true,
			maximizable:true,
			title : '用户信息',
			buttons : [ {
				text : '确定',
				iconCls: 'icon-save',
				plain:true,
				handler : function() {
					if (userForm.find('[name=id]').val() != '') {
						userForm.form('submit', {
							url : proPath+'/userController.do?edit',
							success : function(data) {
								userDialog.dialog('close');
								$.messager.show({
									msg : '用户编辑成功！',
									title : '提示'
								});
								datagrid.datagrid('reload');
							}
						});
					} else {
						userForm.form('submit', {
							url : proPath+'/userController.do?add',
							success : function(data) {
								try {
									var d = $.parseJSON(data);
									if (d) {
										userDialog.dialog('close');
										$.messager.show({
											msg : '用户创建成功！',
											title : '提示'
										});
										datagrid.datagrid('reload');
									}
								} catch (e) {
									$.messager.show({
										msg : '保存错误！',
										title : '提示'
									});
								}
							}
						});
					}
				}
			},{
				text : '重置',
				iconCls:'icon-undo',
				plain:true,
				handler : function() {
					userForm.reset();
				}
			} ]
		}).dialog('close');
	
	datagrid = $('#userGrid').datagrid( {
		url : proPath+'/userController.do?userGrid',
		fitColumns : true,
		fit : true,
		border : false,
		pagination : true,
		idField : 'id',
		pageSize : 30,
		pageList : [ 10, 20, 30, 40, 50 ],
		rownumbers : true,
		sortName : 'id',
		sortOrder : 'asc',
		checkOnSelect : false,
		selectOnCheck : false,
		queryParams:{deptId:'<%=did%>'},
		toolbar : [{
				text : '刷新',
				iconCls : 'icon-reload',
				handler : function() {
					editRow = undefined;
					datagrid.datagrid('reload');
				}
			}, '-', {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					append();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					remove();
				}
			}, '-', {
				text : '编辑',
				iconCls : 'icon-edit',
				handler : function() {
					edit();
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					datagrid.datagrid('unselectAll');
				}
			}, '-' ],
		columns : [ [  {
			field : 'name',
			title : '用户名',
			width : 150,
			sortable : true
		},{
			field : 'loginCode',
			title : '工号',
			width : 150,
			sortable : true
		},{
			field : 'loginPass',
			title : '密码',
			width : 150,
			sortable : true,
			formatter : function(v) {
				return "*******";
			}
			
		},{
			field : 'email',
			title : '邮箱',
			width : 150,
			sortable : true
		},{
			field : 'cardCode',
			title : '证件',
			width : 150,
			sortable : true
		},{
			field : 'deptName',
			title : '部门',
			width : 150,
			sortable : true
		},{
				field : 'roleName',
				title : '角色名称',
				width : 200
			}, {
				field : 'roleId',
				title : '角色编号',
				width : 200,
				hidden : true
			},{
			field : 'status',
			title : '状态',
			width : 150,
			sortable : true,
			formatter : function(v) {
				return "正常";
			}
		},{
			field : 'remark',
			title : '备注',
			width : 150,
			sortable : true
		}] ]

	});

})


	function append() {
		userDialog.dialog('open');
		passwordInput.validatebox({
			required : true
		});
		userForm.find('[name=id]').attr('readonly','readonly');
		userForm.find('[name=name]').removeAttr('readonly');
		userForm.find('[name=loginCode]').removeAttr('readonly');
		userForm.form('clear');
	}
	
	
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length != 1 && rows.length != 0) {
			var names = [];
			for ( var i = 0; i < rows.length; i++) {
				names.push(rows[i].name);
			}
			$.messager.show({
				msg : '只能择一个用户进行编辑！您已经选择了【' + names.join(',') + '】' + rows.length + '个用户',
				title : '提示'
			});
		} else if (rows.length == 1) {
			passwordInput.validatebox({
				required : false
			});
			userForm.find('[name=id]').attr('readonly', 'readonly');
			userForm.find('[name=loginCode]').attr('readonly', 'readonly');
			userDialog.dialog('open');
			userForm.form('clear');
			userForm.form('load', {
				id : rows[0].id,
				name : rows[0].name,
				loginCode : rows[0].loginCode,
				password : '',
				position : rows[0].position,
				email : rows[0].email,
				roleId : sy.getList(rows[0].roleId)
			});
		}
	}

	function remove() {
		var ids = [];
		var rows = datagrid.datagrid('getSelections');
		if (rows.length > 0) {
			$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					
					var url = '<%=request.getContextPath()%>/userController.do?del';
					$.ajax({
						url : url,
						data : {
							ids : ids.join(',')
						},
						cache : false,
						dataType : "json",
						success : function(response) {
							datagrid.datagrid('unselectAll');
							datagrid.datagrid('reload');
							$.messager.show({
								title : '提示',
								msg : '删除成功！'
							});
						}
					});
				}
			});
		} else {
			$.messager.alert('提示', '请选择要删除的记录！', 'error');
		}
	}
	 

    </script>
</head>
<body style="padding:6px; overflow:hidden;">
 
   <input type="hidden" name="deptId" value="<%=deptId%>"/>
   
   <table id="userGrid"></table>
 
   <div id="userDialog" style="display: none;overflow: hidden;width:400px;height:300px;top:0px;left:0px;">
		<form id="userForm" method="post">
			<table class="tableForm" style="font:12px;text-align:left;">
				<tr>
					<th>用户ID</th>
					<td><input name="id" readonly="true"/>
					</td>
				</tr>
				<tr>
					<th>用户名</th>
					<td><input name="name" class="easyui-validatebox" required="true" />
					</td>
				</tr>
				<tr>
					<th>登录名称</th>
					<td><input name="loginCode" class="easyui-validatebox" required="true" />
					</td>
				</tr>
				<tr title="如果不更改密码,请留空!">
					<th>登录密码</th>
					<td><input name="loginPass" type="password" />
					</td>
				</tr>
				<tr>
					<th>职位</th>
					<td><input name="position" class="easyui-validatebox" style="width: 156px;" />
					</td>
				</tr>
				<tr>
					<th>邮箱地址</th>
					<td><input name="email" class="easyui-validatebox" style="width: 156px;" validType="email" invalidMessage="邮箱格式不准确"/>
					</td>
				</tr>
				<tr>
					<th>所属角色</th>
					<td><select name="roleId" style="width: 156px;"></select>
					</td>
				</tr>
			</table>
		</form>
	</div>
	
	
	<div id="userRoleDialog" style="display: none;overflow: hidden;">
		<form id="userRoleForm" method="post">
			<table class="tableForm">
				<input type="hidden" name="userIds" />
				<tr>
					<th>所属角色</th>
					<td><select name="roleId" style="width: 156px;"></select></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
   